package com.mayikt.service.impl;

import com.mayikt.base.BaseApiService;
import com.mayikt.base.BaseResponse;
import com.mayikt.entity.PaymentChannelEntity;
import com.mayikt.mapper.PaymentChannelMapper;
import com.mayikt.service.PaymentVerifyOrderService;
import com.mayikt.strategy.PayStrategy;
import com.mayikt.utils.SpringContextUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PaymentVerifyOrderServiceImpl extends BaseApiService implements PaymentVerifyOrderService {
    @Autowired
    private PaymentChannelMapper paymentChannelMapper;

    @Override
    public BaseResponse<String> verifyOrder(String paymentId, String channelId) {
        // 1.验证参数
        if (StringUtils.isEmpty(paymentId)) {
            return setResultError("paymentId支付id不能为空!");
        }
        if (StringUtils.isEmpty(channelId)) {
            return setResultError("channelId支付id不能为空!");
        }
        // 2.使用策略模式
        PaymentChannelEntity pce = paymentChannelMapper.selectBychannelId(channelId);
        if (pce == null) {
            return setResultError("该渠道已关闭或不存在，请联系管理员");
        }
        // 3.根据beanid 从Spring容器中找到策略类执行
        String payBeanId = pce.getPayBeanId();
        if (StringUtils.isEmpty(payBeanId)) {
            return setResultError("没有配置payBeanId");
        }
        PayStrategy payStrategy = SpringContextUtils.getBean(payBeanId, PayStrategy.class);
        // 调用支付宝查询
        return payStrategy.verifyOrder(pce, paymentId);
    }
}
